package com.jindy.module.order.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.order.domain.Order;
import com.jindy.module.order.domain.OrderApprovalResult;
import com.jindy.module.order.service.IOrderService;
import com.jindy.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2022-10-08
 */
@RestController
@RequestMapping("/module/order")
public class OrderController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private IOrderService orderService;

    /**
     * 查询人员信息
     */
    @Resource
    private ISysUserService userService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('module:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        List<OrderApprovalResult> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('module:order:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        //添加权限
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        order.setUserId(sysUser.getUserId());
        List<OrderApprovalResult> list = orderService.selectOrderList(order);
        ExcelUtil<OrderApprovalResult> util = new ExcelUtil<OrderApprovalResult>(OrderApprovalResult.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:order:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('module:order:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        try {
            orderService.insertOrder(order);
            return AjaxResult.success();
        } catch (Exception e) {
            LOGGER.error("新增租赁订单失败",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:order:update')")
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 修改订单审批状态(8->1)
     */
    @Log(title = "修改订单审批状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:order:updateStatus')")
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody Order order)
    {
        return toAjax(orderService.updateStatus(order));
    }

    /**
     * 修改订单审批状态(0->11)
     */
    @Log(title = "修改订单审批状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:order:updateStatus2')")
    @PutMapping("/updateStatus2")
    public AjaxResult updateStatus2(@RequestBody Order order)
    {
        return toAjax(orderService.updateStatus2(order));
    }

    /**
     * 修改订单审批状态(0->8)
     */
    @Log(title = "修改订单审批状态", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('module:order:updateStatus1')")
    @PutMapping("/updateStatus1")
    public AjaxResult updateStatus1(@RequestBody Order order)
    {
        return toAjax(orderService.updateStatus1(order));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('module:order:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 查询租赁订单
     * @param numberList 订单编号 集合
     * @return
     */
    @PostMapping("/find/by/numbers")
    public AjaxResult findOrdersByNumbers(@RequestBody List<String> numberList)
    {
        return AjaxResult.success(orderService.findOrdersByNumbers(numberList));
    }


}
